package com.cskaoyan.service;


import com.cskaoyan.bean.bo.WxOrderListBO;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.OrderShowListInnerVO;
import com.cskaoyan.bean.vo.OrderShowListVO;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsCommentMapper goodsCommentMapper;
    @Override
    public OrderShowListVO showOrderList(WxOrderListBO wxOrderListBO, User user) {

//        根据传入参数分页
        PageHelper.startPage(wxOrderListBO.getPage(), wxOrderListBO.getSize());

        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause("add_time desc");
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId()).andDeletedEqualTo(false);

//        这个是我们要返回的对象，是用来装count和pageSize以及大list的
        OrderShowListVO orderShowListVO = new OrderShowListVO();
//       这个是最外面那个大的List 用来添加一个订单的对象的

        List<OrderShowListInnerVO> orderShowListInnerVOS = new ArrayList<>();

        OrderShowListInnerVO orderShowListInnerVO = null;
//        这个是状态的对象 专门放每个订单里面的订单状态和可以进行的操作
        HandleOption handleOption = new HandleOption();

        if (wxOrderListBO.getShowType() == 1) {
            criteria.andOrderStatusEqualTo((short) 101);
//            orderShowListInnerVO.setOrderStatusText("未付款");
//            handleOption.setCancel(true);
//            handleOption.setDelete(true);
//            handleOption.setPay(true);
//            orderShowListInnerVO.setHandleOption(handleOption);
        } else if (wxOrderListBO.getShowType() == 2) {
            criteria.andOrderStatusEqualTo((short) 201);
//            orderShowListInnerVO.setOrderStatusText("已付款");
//            handleOption.setCancel(true);
//            handleOption.setDelete(true);
//            handleOption.setRebuy(true);
//            orderShowListInnerVO.setHandleOption(handleOption);
        }
        else if (wxOrderListBO.getShowType()==3){
            criteria.andOrderStatusEqualTo((short) 301);
//            orderShowListInnerVO.setOrderStatusText("已发货");
//            handleOption.setDelete(true);
//            handleOption.setConfirm(true);
//            orderShowListInnerVO.setHandleOption(handleOption);
        }
        else if(wxOrderListBO.getShowType()==4){

            criteria.andOrderStatusEqualTo((short) 401)
            .andCommentsNotEqualTo((short) 0);

//            orderShowListInnerVO.setOrderStatusText("已收货");
//            handleOption.setComment(true);
//            handleOption.setRebuy(true);
//            handleOption.setRefund(true);
//            handleOption.setDelete(true);
//            orderShowListInnerVO.setHandleOption(handleOption);
        }
//        这个是从数据库里面查出的对应类别的所有的订单信息
        List<Order> orders = orderMapper.selectByExample(orderExample);
//        总订单的数量
        long count = orderMapper.countByExample(orderExample);
//        总订单的页数
        long totalPages = (long) Math.round(count/wxOrderListBO.getSize());
        orderMapper.selectByExample(orderExample);

//        先for循环遍历每个订单的信息
        for (Order order : orders) {
//            此处是经典错误
            orderShowListInnerVO =new OrderShowListInnerVO();

            if(order.getOrderStatus()==101){
                orderShowListInnerVO.setOrderStatusText("未付款");
                handleOption.setCancel(true);
                handleOption.setPay(true);
                handleOption.setDelete(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==102){
                orderShowListInnerVO.setOrderStatusText("用户取消");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);

            }
            if(order.getOrderStatus()==103){
                orderShowListInnerVO.setOrderStatusText("系统取消");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==201){
                orderShowListInnerVO.setOrderStatusText("已付款");
                handleOption.setCancel(true);
                handleOption.setPay(false);
                handleOption.setDelete(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==202){
                orderShowListInnerVO.setOrderStatusText("申请退款");
                handleOption.setCancel(true);
                handleOption.setPay(false);
                handleOption.setDelete(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==203){
                orderShowListInnerVO.setOrderStatusText("已退款");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==301){
                orderShowListInnerVO.setOrderStatusText("已发货");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            if(order.getOrderStatus()==401){
                orderShowListInnerVO.setOrderStatusText("用户收货");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(false);
                handleOption.setComment(true);
                handleOption.setConfirm(true);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
//                怎么查一个订单所有的评论
                GoodsCommentExample goodsCommentExample = new GoodsCommentExample();
                GoodsCommentExample.Criteria criteria1 = goodsCommentExample.createCriteria();
//                根据userId找到一个订单所有的评论
                criteria1.andValueIdEqualTo(order.getUserId());
                List<GoodsComment> goodsComments = goodsCommentMapper.selectByExample(goodsCommentExample);
                if(goodsComments.size()>0){
                    handleOption.setComment(false);
                    order.setOrderStatus((short) 402);
                }
            }
            if(order.getOrderStatus()==402){
                orderShowListInnerVO.setOrderStatusText("系统收货");
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setDelete(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setRefund(false);
                handleOption.setRebuy(false);
            }
            orderShowListInnerVO.setHandleOption(handleOption);



//            设置实际支付费用ActualPrice
            orderShowListInnerVO.setActualPrice(order.getActualPrice());
//            设置订单id
            orderShowListInnerVO.setId(order.getId());
            GrouponExample grouponExample = new GrouponExample();
            GrouponExample.Criteria criteria1 = grouponExample.createCriteria();
            criteria1.andUserIdEqualTo(order.getId());
//            根据优惠券里面的订单id查询订单对应的优惠券信息
            List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
//            新建一个Integer类型的数组
            List<Integer> grouponId = new ArrayList<>();
            for (int i = 0; i<groupons.size(); i++) {

                Integer orderGrouponId = groupons.get(i).getOrderId();
                grouponId.add(orderGrouponId);
            }
//            如果这个list不为空 设置IsGroupin状态位为true
            if(grouponId!=null){
                orderShowListInnerVO.setIsGroupin(true);
            }
//            如果这个list为空 设置IsGroupin状态位为false
            orderShowListInnerVO.setIsGroupin(false);
//            通过订单表查询订单编号
            orderShowListInnerVO.setOrderSn(order.getOrderSn());
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            OrderGoodsExample.Criteria criteria2 = orderGoodsExample.createCriteria();
            criteria2.andOrderIdEqualTo(order.getId());
//            根据订单id查询所有的订单商品关系表中的所有信息
//            因为现在是在一个循环里面所以是单个订单的订单商品关系
//            一个订单里面有几个商品的信息,所以要用循环
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

//            在外面new一个List放单个订单所有的商品的
            List<WxGoodsDetail> wxGoodsLists = new ArrayList<>();
            for (OrderGoods orderGood : orderGoods) {
//                创建自己的WxGoodsDeatil对象 里面放单个订单的单个商品信息
                WxGoodsDetail wxGoodsDetail = new WxGoodsDetail();
//                放商品的名字
                wxGoodsDetail.setGoodsName(orderGood.getGoodsName());
//                放商品的货品Id
                wxGoodsDetail.setId(orderGood.getProductId());
//                放商品的数量
                int number = orderGood.getNumber();
                wxGoodsDetail.setNumber((short) number);
//                放商品的图片地址
                wxGoodsDetail.setPicUrl(orderGood.getPicUrl());
//                把Lsit填充进去
                wxGoodsLists.add(wxGoodsDetail);
            }
//            把goodlist放到orderShowListInnerVO对象里面
            orderShowListInnerVO.setGoodsList(wxGoodsLists);

            orderShowListInnerVOS.add(orderShowListInnerVO);
        }

        orderShowListVO.setCount(count);
        orderShowListVO.setTotalPages(totalPages);
        orderShowListVO.setData(orderShowListInnerVOS);
        //List<OrderShowListInnerVO>
        return orderShowListVO;
    }
}
